package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.Scanner;
import java.io.*;


/**
 * Created with IntelliJ IDEA.
 * User: ialbuqu
 * Date: 6/29/13
 * Time: 6:50 PM
 * To change this template use File | Settings | File Templates.
 */

public class BibliotecaApp{

    private IOHandler ioh;
    private Menu menu;
    private String userAnswer = "", login = "", password = "", userName = "", userEmail = "", userPhone = "";
    private DataBase database;
    private Boolean userIsLogged = false, userExist = false;
    private Integer userId = 0;

    public BibliotecaApp(IOHandler ioHandler, DataBase dataBase) {
        this.database = dataBase;
        this.ioh = ioHandler;
        menu = new Menu(ioh);

    }

    public void start() {
        database.startDataBase();

        showWelcomeMessage();
        getAnswer();
    }

    public void showWelcomeMessage(){
        ioh.printMessage("\nWelcome, customer!\n");

    }

    public void showAskIdMessage(){
        ioh.printMessage("Please, inform the ID of the book: ");
    }

    public void showReservedBookMessage(){
        ioh.printMessage("Thank You! Enjoy the book.\n");
    }

    public void showNotReservedBookMessage(){
        ioh.printMessage("Sorry we don't have that book yet.\n");
    }

    private void selectOption(int option) {
        switch (option){
            case 1:
                login();
                break;
            case 2:
                listAllBooks();
                break;
            case 3:
                reserveABook();
                break;
            case 4:
                checkMembershipDetails();
                break;
            case 5:
                listAllMovies();
                break;
            case 9:
                logout();
                break;
            case 0:
                return;
            default:
                ioh.printMessage("Select a valid option!!\n");
        }

    }

    public void getAnswer(){
        while (!userAnswer.equals("0")) {
            menu.showMenu();
            userAnswer = this.ioh.getInput();
            selectOption(Integer.parseInt(userAnswer));
        }
    }

    public void login(){
        if (userIsLogged.equals(false)){
            ioh.printMessage("Please, inform your username: ");
            login = this.ioh.getInput();

            for (UsersList user : this.database.getUsers()){
                if (user.getLogin().equals(login)){
                    userExist = true;
                    if (userExist == true){
                        getPassword();
                    }
                }
            }

            if (userExist == false){
                ioh.printMessage("User doesn't exist, please try again!\n");
                getAnswer();
            }
        }
        else{
            ioh.printMessage("You're already logged in as " + userName + "\n");
        }
    }

    public void getPassword(){
        if (userIsLogged.equals(false)){
            for (UsersList user : this.database.getUsers()){
                ioh.printMessage("Please, inform your password: ");
                password = this.ioh.getInput();
                if (user.getPassword().equals(password)){

                    //saving user data
                    userId = user.getIdUser();
                    userName = user.getName();
                    userEmail = user.getEmail();
                    userPhone = user.getPhone();

                    //keeping the status of login
                    userIsLogged = true;
                    ioh.printMessage("\nWelcome, " + userName + "\n");
                    getAnswer();

                } else{
                    ioh.printMessage("\nWrong password. Do you want to try again?\n\n1 - If you want to try again\n2 - If you want to go back to the menu\nAnswer: ");
                    userAnswer = this.ioh.getInput();
                    if (userAnswer.equals("1")){
                        getPassword();
                    }
                    if (userAnswer.equals("2")){
                        getAnswer();
                    }

                }
            }
        }
    }

    public void listAllBooks(){
        for (BooksList book : this.database.getBooks()) {
            ioh.printMessage(book.getIdBook() + " - " + book.getName() + " (Author: " + book.getAuthor() + ")\n");
        }
        ioh.printMessage("\n");
    }

    public void reserveABook(){

        if (checkLogin().equals(true)){
            showAskIdMessage();
            userAnswer = this.ioh.getInput();
            int idBook = Integer.parseInt(userAnswer);

            for (BooksList book : this.database.getBooks()){
                if (book.getIdBook() == idBook){
                    //Is book available?
                    if (book.getAvailable()){
                        book.setAvailable(false);
                        showReservedBookMessage();
                    }
                    else{
                        showNotReservedBookMessage();
                    }
                }
            }
        }
        else{
            ioh.printMessage("\nOnly clients can reserve a book. Do you want to log on the system?\n");
            ioh.printMessage("1 - Yes (You'll be able to reserve a book)\n2 - No (You can only see the list of books and movies)\nAnswer: ");
            userAnswer = this.ioh.getInput();
            if (userAnswer.equals("1")){
                login();
            }
            else{
                start();
            }
        }
    }

    public void checkMembershipDetails(){
        if (checkLogin().equals(true)){
            ioh.printMessage("Name: " + userName + "\n" + "E-mail: " + userEmail + "\n" + "Phone: " + userPhone + "\n");
        }
        else{
            ioh.printMessage("Please talk to a Librarian. Thank you\n");
        }
    }

    public void listAllMovies(){
        String newLineDivisor = System.getProperty("line.separator");
        String leftAlignFormat = "| %-25s | %-5d | %-18s | %-6s |" + newLineDivisor;

        printHeader(newLineDivisor);

        for (MoviesList movie : this.database.getMovies()){
            printMoviesDetails(leftAlignFormat, movie);
        }

        printEnd();
    }

    public void printHeader(String newLineDivisor){
        String header = "-------------------------------------------------------------------" + newLineDivisor +
                "|           Movie           | Year  |      Director      | Rating |" + newLineDivisor +
                "-------------------------------------------------------------------" + newLineDivisor;
        ioh.printMessage(header);
    }

    public void printEnd(){
        String end = "-------------------------------------------------------------------";
        ioh.printMessage(end);

    }

    public void printMoviesDetails(String leftAlignFormat, MoviesList movie){
        ioh.printMessage(String.format(leftAlignFormat, movie.getName(), movie.getYear(), movie.getDirector(), (movie.getRating() == null) ? "N/A" : movie.getRating()));
    }

    public Boolean checkLogin(){
        if (userIsLogged.equals(true)){
            return true;
        }
        else{
            return false;
        }
    }

    public void logout(){
        userAnswer = ""; login = ""; password = ""; userName = ""; userEmail = ""; userPhone = ""; userIsLogged = false;
        ioh.printMessage("You're not logged in!");
        getAnswer();

    }
}
